package com.ds.dsm.view.config.nav.tree;

import com.ds.common.JDSException;
import com.ds.config.ListResultModel;
import com.ds.config.ResultModel;
import com.ds.dsm.view.config.form.field.FieldFormInfo;
import com.ds.dsm.view.config.tree.TreeActionGroup;
import com.ds.dsm.view.config.tree.TreeDataGroup;
import com.ds.enums.db.MethodChinaName;
import com.ds.esd.custom.annotation.CustomAnnotation;
import com.ds.esd.custom.annotation.GridViewAnnotation;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.bean.MethodConfig;
import com.ds.esd.custom.bean.nav.foldingtree.NavFoldingTreeViewBean;
import com.ds.esd.custom.bean.nav.tree.NavTreeViewBean;
import com.ds.esd.custom.buttonviews.annotation.ButtonViewsAnnotation;
import com.ds.esd.custom.form.pop.PopTreeViewBean;
import com.ds.esd.custom.module.annotation.DynLoadAnnotation;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.custom.nav.tab.buttonviews.NavButtonViewsViewAnnotation;
import com.ds.esd.custom.tree.CustomTreeViewBean;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.view.ViewEntityConfig;
import com.ds.esd.dsm.view.field.FieldFormConfig;
import com.ds.esd.tool.ui.component.tab.SideBarStatusType;
import com.ds.esd.tool.ui.enums.BarLocationType;
import com.ds.esd.tool.ui.enums.Dock;
import com.ds.web.util.PageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(path = "/dsm/view/config/nav/tree/")
@MethodChinaName(cname = "树形配置")
@ButtonViewsAnnotation(barLocation = BarLocationType.top, sideBarStatus = SideBarStatusType.expand, barSize = "3em")
public class NavTreeConfigView {


    @CustomAnnotation(hidden = true, pid = true)
    public String entityClassName;
    @CustomAnnotation(hidden = true, pid = true)
    public String sourceClassName;
    @CustomAnnotation(uid = true, hidden = true)
    private String methodName;

    @CustomAnnotation(pid = true, hidden = true)
    private String projectId;

    @CustomAnnotation(pid = true, hidden = true)
    private String viewInstId;
    @CustomAnnotation(pid = true, hidden = true)
    private String domainId;

    public NavTreeConfigView() {

    }


    @RequestMapping(method = RequestMethod.POST, value = "FieldList")
    @GridViewAnnotation
    @ModuleAnnotation(imageClass = "spafont spa-icon-c-comboinput", caption = "表单字段")
    @APIEventAnnotation(autoRun = true)
    @ResponseBody
    public ListResultModel<List<FieldFormInfo>> getFieldList(String sourceClassName, String methodName, String domainId, String viewInstId) {
        ListResultModel<List<FieldFormInfo>> cols = new ListResultModel();
        try {
            ViewEntityConfig config = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName,  viewInstId);
            MethodConfig customMethodAPIBean = config.getSourceConfig().getMethodByName(methodName);
            CustomTreeViewBean customTreeViewBean = null;

            if (customMethodAPIBean.getView() instanceof NavTreeViewBean) {
                NavTreeViewBean baseViewBean = (NavTreeViewBean) customMethodAPIBean.getView();
                customTreeViewBean = baseViewBean.getTreeViewBean();
            } else if (customMethodAPIBean.getView() instanceof NavFoldingTreeViewBean) {
                NavFoldingTreeViewBean baseViewBean = (NavFoldingTreeViewBean) customMethodAPIBean.getView();
                customTreeViewBean = baseViewBean.getTreeViewBean();
            } else if (customMethodAPIBean.getView() instanceof PopTreeViewBean) {
                PopTreeViewBean baseViewBean = (PopTreeViewBean) customMethodAPIBean.getView();
                customTreeViewBean = baseViewBean.getTreeViewBean();
            } else if (customMethodAPIBean.getView() instanceof CustomTreeViewBean) {
                customTreeViewBean = (CustomTreeViewBean) customMethodAPIBean.getView();
            }

            List<String> fieldNames = customTreeViewBean.getFieldNames();
            List<FieldFormConfig> fields = new ArrayList<>();
            for (String fieldName : fieldNames) {
                FieldFormConfig fieldFormConfig = (FieldFormConfig) customTreeViewBean.getFieldConfigMap().get(fieldName);
                if (fieldFormConfig != null) {
                    fieldFormConfig.setEntityClassName(customMethodAPIBean.getEntityClassName());
                    fields.add(fieldFormConfig);
                }
            }
            cols = PageUtil.getDefaultPageList(fields, FieldFormInfo.class);
        } catch (JDSException e) {
            e.printStackTrace();
        }
        return cols;
    }

    @MethodChinaName(cname = "动作事件")
    @RequestMapping(method = RequestMethod.POST, value = "TreeActionGroup")
    @NavButtonViewsViewAnnotation
    @DynLoadAnnotation
    @ModuleAnnotation(dock = Dock.fill, caption = "动作事件", imageClass = "spafont spa-icon-action")
    @ResponseBody
    public ResultModel<TreeActionGroup> geTreeInfoGroup(String sourceClassName, String methodName, String domainId, String viewInstId) {
        ResultModel<TreeActionGroup> result = new ResultModel<TreeActionGroup>();
        return result;

    }


    @RequestMapping(method = RequestMethod.POST, value = "TreeDataGroup")
    @DynLoadAnnotation
    @NavButtonViewsViewAnnotation
    @ModuleAnnotation(dock = Dock.fill, caption = "窗体配置", imageClass = "spafont spa-icon-values")
    @ResponseBody
    public ResultModel<TreeDataGroup> getTreeDataGroup(String sourceClassName, String methodName, String domainId, String viewInstId) {
        ResultModel<TreeDataGroup> result = new ResultModel<TreeDataGroup>();
        return result;

    }

    public String getDomainId() {
        return domainId;
    }

    public void setDomainId(String domainId) {
        this.domainId = domainId;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public String getEntityClassName() {
        return entityClassName;
    }

    public void setEntityClassName(String entityClassName) {
        this.entityClassName = entityClassName;
    }

    public String getSourceClassName() {
        return sourceClassName;
    }

    public void setSourceClassName(String sourceClassName) {
        this.sourceClassName = sourceClassName;
    }

    public String getProjectId() {
        return projectId;
    }

    public void setProjectId(String projectId) {
        this.projectId = projectId;
    }

    public String getViewInstId() {
        return viewInstId;
    }

    public void setViewInstId(String viewInstId) {
        this.viewInstId = viewInstId;
    }
}
